﻿using Framework.Common;
using Framework.Common.Exceptions;
using Framework.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http.Filters;
using System.Web.Mvc;
using Framework.MQS;

namespace Application.WebAPI.Filter
{
    /// <summary>
    /// 全局异常处理
    /// </summary>
    public class ExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var exception = actionExecutedContext.Exception;
            HttpResponseMessage httpResponseMessage;
            var dataResult = new DataResult();
            LogManager.getInstance().WriteException("SysLog", exception);

            string errors = String.Format("日期:{0}\r\n{1}\r\n", DateTime.Now.ToString(), exception.Message + ":" + exception.Source + ":" + exception.StackTrace);
            //AliyunQueue queue = new AliyunQueue();
            //queue.Push(errors);

            if (exception is BusinessException)
            {
                dataResult.Code = ResponseStatusCode.Error;
                dataResult.Msg = exception.Message;
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, dataResult);
            }
            else if (exception is AccountLoginInOtherPlacesException)
            {
                dataResult.Code = ResponseStatusCode.Error_NeedReLogin;
                dataResult.Msg = exception.Message;
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.Unauthorized, dataResult);
            }
            else if (exception is AuthorizeException)
            {
                dataResult.Code = ResponseStatusCode.Error_Unauthorized;
                dataResult.Msg = exception.Message;
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.Unauthorized, dataResult);
            }
            else if (exception is DataOperationPermissions)
            {
                dataResult.Code = ResponseStatusCode.Error_Forbidden;
                dataResult.Msg = exception.Message;
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.Forbidden, dataResult);
            }
            else if (exception is DataValidationException)
            {
                dataResult.Code = ResponseStatusCode.Error;
                dataResult.Msg = exception.Message;
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, dataResult);
            }
            else if (exception is ArgumentException)
            {
                dataResult.Code = ResponseStatusCode.Error;
                dataResult.Msg = exception.Message;
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, dataResult);
            }
            else if (exception is QueryArgumentException)
            {
                dataResult.Code = ResponseStatusCode.Error;
                dataResult.Msg = exception.Message;
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, dataResult);
            }
            else
            {
                dataResult.Code = ResponseStatusCode.Error;
                dataResult.Msg = "系统异常，请联系管理员";
                httpResponseMessage = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, dataResult);
            }
            actionExecutedContext.Response = httpResponseMessage;
            base.OnException(actionExecutedContext);
        }
    }
}